package de.tum.in.eist.poll.client;

import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.pub.shared.Lecture;
import de.tum.in.eist.poll.pub.shared.Student;
import de.tum.in.eist.poll.pub.shared.User;
/**
 * This class provides a panel, which allows the user to choose 
 * between Commiting a lecture and watching the statistics.
 */
public class SelectLectureView extends VerticalPanel {
	private int method = 0;
	private ListBox lecturesSelect = new ListBox();
	private List<Lecture> lectures;
	private User user;
	private HorizontalPanel selectPanel = new HorizontalPanel();
	private RadioButton commit = new RadioButton("select", "Commit on lecture");
	private RadioButton stats = new RadioButton("select", "See statistics");
	
	/**
	 * Does the connection for showing the available lectures.
	 */
	public SelectLectureView(){
		this.user = EIST_POLL.currentUser;
		if(user == null){
			Window.Location.reload();
		}
		AsyncCallback<List<Lecture>> callback = new AsyncCallback<List<Lecture>>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(List<Lecture> result) {
				buildView(result);				
			}
		};
		
		EIST_POLL.service.getLectures(user, callback);
	}
	
	/**
	 * Offers an interface for the user to choose a lecture, which he wants to evaluate.
	 * 
	 * @param lectures is a list of all available lectures.
	 */
	private void buildView(final List<Lecture> lectures){
		this.lectures = lectures;
		ListIterator<Lecture> iterator = this.lectures.listIterator();
		lecturesSelect.addItem("select the Lecture");
		while(true){
			try{
				lecturesSelect.addItem(iterator.next().getName(), ""+iterator.previousIndex());
			}
			catch(NoSuchElementException e){
				break;
			}
		}
		
		lecturesSelect.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				int indx = lecturesSelect.getSelectedIndex();
				RootPanel root = RootPanel.get("main");
				root.clear();
				if(method == 0){
					Lecture lecture = lectures.get(new Integer(lecturesSelect.getValue(indx)));
					root.add(new giveFeedbackView(lecture, (Student)user));
				}
				else{
					root.add(new StatisticView(lectures.get(new Integer(lecturesSelect.getValue(indx)))));
				}
			}
		});
		
		commit.setValue(true);
		RadioHandler handler = new RadioHandler();
		commit.addClickHandler(handler);
		stats.addClickHandler(handler);
		this.selectPanel.add(commit);
		this.selectPanel.add(stats);
		this.add(selectPanel);
		this.add(lecturesSelect);
	}
	
	/**
	 * This class takes care of the onClick of the "commit"-Button
	 */
	class RadioHandler implements ClickHandler{
		
		/**
		 * The event tests if the event referse to the commit or to the stats
		 */
		@Override
		public void onClick(ClickEvent event) {
			if(event.getSource().equals(commit)){
				method = 0;
			}
			else{
				method = 1;
			}
		}
		
	}
}
